package cn.gz.zoomtech.common;

/**
 *  通信格式转换 
 *  Java和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时，需要进行相应的转换 　　
 *  高、低字节之间的转换 　　
 *  windows的字节序为低字节开头 　　
 *  linux,unix的字节序为高字节开头 　　
 *  java则无论平台变化，都是高字节开头 　
 * @author Administrator
 *
 */
public class FormatTransfer {
	

	/**
	 * 
	* package_name: cn.gz.zoomtech.common
	* file_name:    FormatTransfer.java
	* description: 将int转为低字节在前，高字节在后的byte数组 
	* 2011-9-27下午03:39:48
	* Author: chenhui
	 * @param int n
	 * @return return byte[] 
	 */
	public static byte[] toLH(int n) {
		byte[] b = new byte[4];
		b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		b[2] = (byte) (n >> 16 & 0xff);
		b[3] = (byte) (n >> 24 & 0xff);
		return b;
		}
	/**
	　　  * 将short转为低字节在前，高字节在后的byte数组
	　　  * @param n short
	　　  * @return byte[]
	　　  */
	public static byte[] toLH(short n) {
		byte[] b = new byte[2];
        b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		return b;
    }
	
	/**
	 * 
	* package_name: cn.gz.zoomtech.common
	* file_name:    FormatTransfer.java
	* description: 将int转为高字节在前，低字节在后的byte数组
	* 2011-9-27下午03:53:21
	* Author: chenhui
	 * @param n
	 * @return
	 */
	public static byte[] toHH(int n) {
		byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b; 
	}
	
	/**
	  * 将低字节数组转换为int
	  * @param b byte[]
	  * @return int
	  */
	public static int lBytesToInt(byte[] b) {
	  int s = 0;
	  for (int i = 0; i < 3; i++) {
	    if (b[3-i] >= 0) {
	    s = s + b[3-i];
	    } else {
	    s = s + 256 + b[3-i];
	    }
	    s = s * 256;
	  }
	  if (b[0] >= 0) {
	    s = s + b[0];
	  } else {
	    s = s + 256 + b[0];
	  }
	  return s;
	} 
	
	/**
	　　  * 低字节数组到short的转换
	　　  * @param b byte[]
	　　  * @return short
	　　  */
	public static short lBytesToShort(byte[] b) {
		int s = 0;
		if (b[1] >= 0) {
			s = s + b[1];
		} else {
		   s = s + 256 + b[1];
		}
		s = s * 256;
		if (b[0] >= 0) {
			s = s + b[0];
		} else {
          s = s + 256 + b[0];
        }
		short result = (short)s;
        return result;
    } 

	
	

	/**
	 * 将字节数组转换为int
	 * @param buffer byte[]
	 * @return int
	 */
 public static final int getIntFromByte(byte[] buffer)   
	{   
	 int a = 0;   
	 a = (0xff000000&(((int)buffer[0])<<24)   
			 |0x00ff0000&(((int)buffer[1])<<16)   
			 |0x0000ff00&(((int)buffer[2])<<8)   
			 |0x000000ff&(((int)buffer[3])<<0));   
	return a;   
   } 

	/**
	 * 将int转换为字节数组
	 * @param buffer byte[]
	 * @return int
	 */
 public static  byte[] getBytesFromInt(int length)   
	{   
	 byte b[] = new byte[4];   
	 b[0] = (byte)(length>>24 & 0xFF);   
	 b[1] = (byte)(length>>16 & 0xFF);   
	 b[2] = (byte)(length>>8 & 0xFF);   
	 b[3] = (byte)(length>>0 & 0xFF);   
	 return b;              
	}
 
	

	/**
	 * package_name: cn.gz.zoomtech.common
	 * file_name:    FormatTransfer.java
	 * description: 
	 * 2011-9-27下午03:16:48
	 * Author: chenhui
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		FormatTransfer ft = new FormatTransfer();
//		int n = 39;
//		System.out.println("n is:"+n);
//		byte[] n_b= FormatTransfer.toLH(n);
	    
		
//		System.out.println("n_b.length is:"+n_b.length);
		
//		byte[] n_bHH= FormatTransfer.toHH(n);
		
		
//		int n_b_int= ByteUtil.toInt(n_bHH);
		
//		System.out.println("n_b_int is:"+n_b_int);
		
//		System.out.println("n_bHH is:"+n_bHH);

	}

}
